		TITLE	DERIVATE - Copyright (c) 1994 SLR Systems

		INCLUDE	MACROS

if	fg_cvpack

		INCLUDE	CVTYPES

		PUBLIC	DO_DERIVATIONS


		.DATA

		EXTERNDEF	FIRST_DERIV_BLK:DWORD,CV_SPECIAL_BLOCK:DWORD

		EXTERNDEF	CV_LTYPE_GARRAY:STD_PTR_S,CV_GTYPE_GARRAY:STD_PTR_S,CV_LTYPE_STUFF:ALLOCS_STRUCT

		.CODE	CVPACK_TEXT

		EXTERNDEF	CV_LTYPE_POOL_GET:PROC,RELEASE_BLOCK:PROC,GET_NEW_LOG_BLK:PROC,RELEASE_CV_LTYPE_GARRAY:PROC
		EXTERNDEF	FLUSH_DERIVATIVES:PROC,SAY_VERBOSE:PROC,_release_minidata:proc,CONVERT_CV_LTYPE_GTYPE_A:PROC


CVCG_STRUCT	STRUC

_CVCG_GINDEX		DD	?		;CLASS GINDEX
_CVCG_PREV		DD	?
_CVCG_NEXT_HASH		DD	?		;NEXT CLASS THIS HASH
_CVCG_N_DERIVS		DD	?		;# OF DERIVATIVES
_CVCG_SUB_CLASSES	DD	4 DUP(?)	;ROOM FOR 4

_CVCG_MAX_CNT		EQU	_CVCG_SUB_CLASSES
_CVCG_DERIV_PTR		EQU	_CVCG_SUB_CLASSES+4
;_CVCG_DERIV_PTR_BASE	EQU	_CVCG_SUB_CLASSES+8

CVCG_STRUCT	ENDS



DO_DERIV_VARS		STRUC

FIRST_CVCG_BP		DD	?
LAST_CVCG_BP		DD	?
CVCG_HASH_BP		DD	?
CVCG_HASH_LOG_BP	DD	?
CVG_DERIV_BLK_BP	DD	?
CVG_DERIV_LIMIT_BP	DD	?

DO_DERIV_VARS		ENDS


FIX	MACRO	X

X	EQU	([EBP-SIZE DO_DERIV_VARS].(X&_BP))

	ENDM


FIX	LAST_CVCG
FIX	CVCG_HASH
FIX	CVCG_HASH_LOG
FIX	CVG_DERIV_BLK
FIX	CVG_DERIV_LIMIT


DO_DERIVATIONS	PROC
		;
		;PROCESS DERIVATION STUFF STORED IN FIRST_DERIV_BLK LIST
		;
		CMP	FIRST_DERIV_BLK,0
		JNZ	L0$

		RET

L0$:
		MOV	EAX,OFF DOING_DERIVATIONS_MSG
		CALL	SAY_VERBOSE

		CALL	FLUSH_DERIVATIVES

		PUSHM	EBP,EDI,ESI,EBX

		MOV	EBP,ESP
		SUB	ESP,SIZE DO_DERIV_VARS
		ASSUME	EBP:PTR DO_DERIV_VARS

		XOR	EAX,EAX

		MOV	LAST_CVCG,EAX
		CALL	GET_NEW_LOG_BLK

		MOV	CVCG_HASH_LOG,EAX
		MOV	EDI,EAX

		XOR	EAX,EAX
		MOV	ECX,PAGE_SIZE/4

		REP	STOSD

		MOV	CVCG_HASH,SMALL_SYM_HASH
		;
		;
		;
		MOV	ESI,FIRST_DERIV_BLK
L1$:
		LEA	EAX,[ESI+PAGE_SIZE-4]

		MOV	CVG_DERIV_BLK,ESI
		MOV	CVG_DERIV_LIMIT,EAX

L2$:
		MOV	ECX,[ESI]
		ADD	ESI,4

		TEST	ECX,ECX
		JZ	L41$

		PUSHM	ESI,ECX

		SHR	ECX,16
		MOV	ESI,CVCG_HASH_LOG

		MOV	EDX,ECX
		AND	ECX,PAGE_SIZE/4-1

		MOV	EAX,[ESI+ECX*4]
		LEA	ESI,[ESI+ECX*4 - CVCG_STRUCT._CVCG_NEXT_HASH]
L21$:
		TEST	EAX,EAX
		JZ	L25$

		ASSUME	EAX:PTR CVCG_STRUCT,ESI:PTR CVCG_STRUCT

		MOV	ESI,EAX
		MOV	ECX,[EAX]._CVCG_GINDEX

		CMP	EDX,ECX
		JZ	L29$

		MOV	EAX,[EAX]._CVCG_NEXT_HASH
		JMP	L21$

		ASSUME	EAX:NOTHING

L25$:
		MOV	EAX,SIZEOF CVCG_STRUCT
		CALL	CV_LTYPE_POOL_GET

		MOV	EBX,LAST_CVCG
		MOV	[ESI]._CVCG_NEXT_HASH,EAX

		MOV	ESI,EAX
		MOV	LAST_CVCG,EAX

		MOV	[EAX].CVCG_STRUCT._CVCG_PREV,EBX
		XOR	EAX,EAX

		MOV	[ESI]._CVCG_GINDEX,EDX
		MOV	[ESI]._CVCG_N_DERIVS,EAX

		MOV	[ESI]._CVCG_NEXT_HASH,EAX

L29$:
		;
		;INSERTION-SORT THESE FOR MAX PACKING OF DERIVATIVE RECORDS...
		;
		MOV	EAX,[ESI]._CVCG_N_DERIVS
		POP	EDX

		INC	EAX
		AND	EDX,0FFFFH

		MOV	[ESI]._CVCG_N_DERIVS,EAX
L3$:
		MOV	ECX,EAX

		CMP	EAX,5
		JAE	L35$

		LEA	ESI,[ESI]._CVCG_SUB_CLASSES
		JMP	L305$

		ASSUME	ESI:NOTHING

L30$:
		MOV	EAX,[ESI]
		ADD	ESI,4

		CMP	EAX,EDX
		JA	L31$
L305$:
		DEC	ECX
		JNZ	L30$

		MOV	[ESI],EDX
		JMP	L4$

L31$:

		MOV	[ESI-4],EDX
		MOV	EDX,EAX

		MOV	EAX,[ESI]
		ADD	ESI,4

		DEC	ECX
		JNZ	L31$

		MOV	[ESI-4],EDX
		JMP	L4$

		ASSUME	ESI:PTR CVCG_STRUCT
L35$:
		;
		;BIG, OR GOING BIG...
		;
		MOV	EAX,[ESI]._CVCG_MAX_CNT
		JZ	L38$

		CMP	EAX,ECX
		JB	L37$
L36$:
		MOV	ESI,[ESI]._CVCG_DERIV_PTR
		JMP	L305$

L37$:
		PUSH	ECX
		MOV	ECX,EAX			;DWORDS TO MOVE

		ADD	EAX,EAX			;DOUBLE IT IN SIZE

		MOV	[ESI]._CVCG_MAX_CNT,EAX	;TOTAL IN NEW SPACE

		SHL	EAX,2			;BYTES TO ALLOCATE
		CALL	CV_LTYPE_POOL_GET

		PUSHM	ESI,EAX

		MOV	EDI,EAX
		MOV	EAX,[ESI]._CVCG_DERIV_PTR

		MOV	[ESI]._CVCG_DERIV_PTR,EDI
		MOV	ESI,EAX

		REP	MOVSD

		POPM	EAX,ESI

		POP	ECX
		JMP	L36$

L38$:
		;
		;FIRST REF >4
		;
		MOV	EAX,32
		CALL	CV_LTYPE_POOL_GET

		DEC	ECX			;DWORDS TO MOVE
		MOV	EDI,EAX

		PUSH	ESI
		LEA	ESI,[ESI]._CVCG_SUB_CLASSES

		REP	MOVSD

		POP	ESI

;		MOV	[ESI]._CVCG_DERIV_PTR_BASE,EAX
;		MOV	[ESI]._CVCG_DERIV_PTR,EDI
		MOV	[ESI]._CVCG_DERIV_PTR,EAX

		MOV	[ESI]._CVCG_MAX_CNT,8	;TOTAL IN NEW SPACE

		MOV	ECX,5
		JMP	L36$

L4$:
		ASSUME	ESI:NOTHING

		POP	ESI
		MOV	EAX,CVG_DERIV_LIMIT

		CMP	ESI,EAX
		JB	L2$

		MOV	EAX,CVG_DERIV_BLK
		MOV	ESI,[ESI]

		CALL	RELEASE_BLOCK

		JMP	L1$

L41$:
		;
		;NOW, FOR EACH CLASS, INSTALL ITS DERIVATION RECORD.
		;
		MOV	EAX,CVCG_HASH_LOG
		INSTALL_POINTER_GINDEX	CV_LTYPE_GARRAY

		MOV	ESI,LAST_CVCG
L5$:
		TEST	ESI,ESI
		JZ	L9$

		ASSUME	ESI:PTR CVCG_STRUCT
		;
		;BUILD A VALID LTYPE RECORD, THEN CALL HELPER FUNCTION IN CVTYPCON
		;
		MOV	ECX,[ESI]._CVCG_PREV
		MOV	EAX,[ESI]._CVCG_GINDEX	;BASE CLASS

		PUSHM	ECX,EAX

		MOV	EDI,CVCG_HASH_LOG
		MOV	ECX,CV_LTYPE_STRUCT._CV_LTYPE_LENGTH/4

		XOR	EAX,EAX
		MOV	EDX,[ESI]._CVCG_N_DERIVS

		REP	STOSD

		MOV	[EDI+4],DL
		MOV	ECX,EDX

		MOV	[EDI+5],DH
		ADD	EDX,EDX

		ADD	EDX,4+I_LF_DERIVED*64K
		LEA	ESI,[ESI]._CVCG_SUB_CLASSES
		ASSUME	ESI:NOTHING

		MOV	[EDI],EDX
		ADD	EDI,6

		CMP	ECX,5
		JB	L51$

		MOV	ESI,DPTR [ESI+4]

L51$:
		MOV	EAX,[ESI]
		ADD	ESI,4

		MOV	[EDI],AX
		ADD	EDI,2

		DEC	ECX
		JNZ	L51$

		MOV	[EDI],ECX
		POP	EBX			;GINDEX
		;
		;STORE IN GLOBAL TYPE TABLE
		;
		MOV	EAX,1000H
		CALL	CONVERT_CV_LTYPE_GTYPE_A

		SUB	EBX,1000H-1
		MOV	ECX,EAX

		CONVERT	ESI,EBX,CV_GTYPE_GARRAY
		ASSUME	ESI:PTR CV_GTYPE_STRUCT
		MOV	[ESI]._CV_GTYPE_WORD4,CX
L8$:
		;
		;DO NEXT
		;
		POP	ESI
		JMP	L5$

L9$:
		MOV	EAX,CVCG_HASH_LOG
		CALL	RELEASE_BLOCK

		MOV	EAX,OFF CV_LTYPE_STUFF
		push	EAX
		call	_release_minidata
		add	ESP,4

		CALL	RELEASE_CV_LTYPE_GARRAY

		MOV	ESP,EBP

		POPM	EBX,ESI,EDI,EBP

		RET


DO_DERIVATIONS	ENDP

		.CONST

DOING_DERIVATIONS_MSG DB	SIZEOF DOING_DERIVATIONS_MSG-1,'Doing Class derivations',0DH,0AH

endif

		END

